Option Explicit

const L_SiteName = "GenericSite"
const L_ResourceName = "GenericResource"
const L_ResourceType = "GenericResourceType"
const L_DBServerName = "MJOSHI06"
const L_ResourceDBName = "GenericSite_DB"
const L_GenericCreateScriptName = "GenericScriptCreate"
const L_GenericDropScriptName = "GenericScriptDrop"

Dim genericConnectionString
Dim globalConfig
Dim siteConfig

genericConnectionString = GetConnectionString (L_ResourceDBName, L_DBServerName)

Set globalConfig = CreateObject("Commerce.GlobalConfig")
globalConfig.Initialize
globalConfig.CreateSiteConfig (L_SiteName)

Set siteConfig = CreateObject ("Commerce.SiteConfig")
siteConfig.Initialize (L_SiteName)

' create the resource database
CreateDatabase L_ResourceDBName, L_DBServerName

' the resource type must be defined in the admin db or the below line will fail
' to define the resource type, execute genericresourcedefinition.sql against your admin database
siteConfig.CreateComponentConfig L_ResourceName, L_ResourceType

siteConfig.Fields(L_ResourceName).Value.Fields("s_ProgidPUP") = "Commerce.GenericPup"
' the connection string property must be named as in the below format for GenericPup to work
siteConfig.Fields(L_ResourceName).Value.Fields("connstr_db_" & L_ResourceName).Value = genericConnectionString

'execute the schema scripts so that when the site is packed, they get picked up
ExecuteSqlScript L_GenericCreateScriptName & ".sql", L_ResourceDBName, L_DBServerName

' the list of tables that contain data that we want to pack/unpack along with this resource
siteConfig.Fields(L_ResourceName).Value.Fields("s_PUPParam1") = "GenericTable1,GenericTable2"

'save the schema in the admin database
LoadSqlScriptIntoAdminDB siteConfig, L_ResourceName, L_GenericCreateScriptName & ".sql", L_GenericCreateScriptName
LoadSqlScriptIntoAdminDB siteConfig, L_ResourceName, L_GenericDropScriptName & ".sql", L_GenericDropScriptName

'the list of scripts that should be executed when unpacking the resource
siteConfig.Fields(L_ResourceName).Value.Fields("s_PUPParam2") = L_GenericCreateScriptName

'the script that should be executed to drop the resource
siteConfig.Fields(L_ResourceName).Value.Fields("s_PUPParam3") = L_GenericDropScriptName

siteConfig.SaveConfig


Sub LoadSqlScriptIntoAdminDB (oSiteConfig, sResource, sFilePath, sScriptName)
    Dim oFS
    Dim oFile
    Dim sScriptContents

    const ForReading = 1
    const TristateUseDefault = -2

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.OpenTextFile(sFilePath, ForReading, False, TristateUseDefault)
    sScriptContents = oFile.ReadAll()
    oFile.Close

    oSiteConfig.PutSQLScript sResource, sScriptName, sScriptContents
End Sub

Sub ExecuteSqlScript (sFilePath, sDBName, sDBServer)
	Dim sCmdLine
	Dim WshShell
	
	sCmdLine = "osql -E -S " + sDBServer + " -d " + sDBName + " -i " + sFilePath
	Set WshShell = Wscript.CreateObject("Wscript.Shell")
	WshShell.Run sCmdLine, 1, True	
End Sub 

Sub CreateDatabase (sDBName, sDBServer)
    Dim objADO

    set objADO = CreateObject("ADODB.Connection")
    objADO.Open GetConnectionString("master", sDBServer)
	On Error Resume Next
    objADO.Execute("Drop Database " & sDBName)
    On Error GoTo 0
    objADO.Execute("Create Database " & sDBName)
End Sub


Function GetConnectionString (sDBName, sDBServer)
	GetConnectionString = _
		"Provider=SQLOLEDB.1" & _
		";Integrated Security=SSPI" & _
		";Persist Security Info=True" & _
		";Initial Catalog=" & sDBName & _
		";Data Source=" & sDBServer 
End Function
